import {useMessageStore} from "@/hooks/im/message";
import { useConversationStore } from "@/hooks/im/conversation";
import { OpenIMEmitter } from "open-im-sdk-rn";
import {
    ConversationItem,
} from "open-im-sdk-rn/lib/typescript/types/entity";
import { useEffect } from "react";
import { Provider, useSelector, useDispatch } from 'react-redux';
import { RootState } from '@/store/rootReducer';
import { setNewMessage } from '@/store/slices/imSlice';




export function useGlobalEvent() {
    const dispatch = useDispatch();

    // conversation
    const updateConversationList = useConversationStore((state) => state.updateConversationList);
    const updateUnReadCount = useConversationStore((state) => state.updateUnReadCount);

    //message
    const pushNewMessage = useMessageStore((state) => state.pushNewMessage);


    const conversationChnageHandler = (data: ConversationItem[]) => {
        console.log('changge', data);

        updateConversationList(data, "filter");
    };
    const newConversationHandler = (data: ConversationItem[]) => {
        updateConversationList(data, "push");
    };
    const totalUnreadChangeHandler = (data: number) => {
        console.log('totalUnreadChangeHandler', data);
        updateUnReadCount(data);
    };

    const newMessageHandler = (data) => {
        console.log('datadatadatadatadatadata', data);
        data.forEach((item) => {
            pushNewMessage(item);
            dispatch(setNewMessage('newMessage', item))
        })

    };


    const setIMListener = () => {
        // conversation
        OpenIMEmitter.addListener("onConversationChanged", conversationChnageHandler);
        OpenIMEmitter.addListener("onNewConversation", newConversationHandler);
        OpenIMEmitter.addListener("onTotalUnreadMessageCountChanged", totalUnreadChangeHandler);
        // OpenIMEmitter.addListener("onRecvNewMessage", newMessageHandler);
        OpenIMEmitter.addListener("onRecvNewMessages", newMessageHandler);
    };

    const disposeIMListener = () => {
        const eventNames = [
            "onSelfInfoUpdated",
            "onConnecting",
            "onConnectFailed",
            "onConnectSuccess",
            "onKickedOffline",
            "onUserTokenExpired",
            "onSyncServerStart",
            "onSyncServerFinish",
            "onSyncServerFailed",
            "onSyncServerProgress",
            "onRecvNewMessage",
            "onRecvNewMessages",
            "onNewRecvMessageRevoked",
            "onConversationChanged",
            "onNewConversation",
            "onTotalUnreadMessageCountChanged",
            "onFriendInfoChanged",
            "onFriendAdded",
            "onFriendDeleted",
            "onBlackAdded",
            "onBlackDeleted",
            "onJoinedGroupAdded",
            "onJoinedGroupDeleted",
            "onGroupInfoChanged",
            "onGroupMemberAdded",
            "onGroupMemberDeleted",
            "onGroupMemberInfoChanged",
            "onFriendApplicationAdded",
            "onFriendApplicationAccepted",
            "onFriendApplicationRejected",
            "onGroupApplicationAdded",
            "onGroupApplicationAccepted",
            "onGroupApplicationRejected",
        ];
        eventNames.forEach((eventName) => {
            OpenIMEmitter.removeAllListeners(eventName);
        });
    };

    useEffect(() => {
        setIMListener();
        console.info("useGlobalEvent start");
        return () => {
            console.info("useGlobalEvent destroy");
            disposeIMListener();
        };
    }, []);
}
